package com.kaikeba.dao.imp;

import com.kaikeba.bean.Admin;
import com.kaikeba.dao.BaseAdminDao;
import com.kaikeba.util.DruidUtil;
import com.mysql.cj.protocol.Resultset;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class AdminDaoMysql implements BaseAdminDao {

    private final String SQL_UPDATE_LOGIN_DATE = "UPDATE ADMIN SET LOGINTIME=?, LOGINIP=? WHERE USERNAME=?";
    private final String SQL_LOGIN = "SELECT ID FROM ADMIN WHERE USERNAME=? AND PASSWORD=?";
    private final String SQL_FIND_BY_PHONE = "SELECT ID, USERNAME, PASSWORD, PHONE, LOGINIP, LOGINTIME, CREATETIME FROM ADMIN WHERE PHONE=?";

    /**
     * 用于更新管理登录的时间和ip地址
     *
     * @param username 管理员用户名
     * @param date     管理员登录日期
     * @param ip       管理员登录ip地址
     */
    @Override
    public void updataLogin(String username, Date date, String ip) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        try {
            state = conn.prepareStatement(SQL_UPDATE_LOGIN_DATE);
            state.setDate(1, new java.sql.Date(date.getTime()));
            state.setString(2, ip);
            state.setString(3, username);
            state.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(conn, state, null);
        }
    }
    /**
     * 用于管理员进行登录
     *
     * @param username 管理员用户名
     * @param password 管理员密码
     * @return 登录结果，true表示成功，false表示失败
     */
    @Override
    public boolean login(String username, String password) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet rs = null;
        try {
            state = conn.prepareStatement(SQL_LOGIN);
            state.setString(1, username);
            state.setString(2, password);
            rs = state.executeQuery();
            return rs.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(conn, state, rs);
        }
        return false;
    }

    @Override
    public Admin findByPhone(String phone) {
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet rs = null;
        try {
            state = conn.prepareStatement(SQL_FIND_BY_PHONE);
            state.setString(1, phone);
            rs = state.executeQuery();
            if(rs.next()){
                Admin admin = new Admin();
                admin.setId(rs.getInt("ID"));
                admin.setUsername(rs.getString("USERNAME"));
                admin.setPassword(rs.getString("PASSWORD"));
                admin.setPhone(rs.getString("PHONE"));
                admin.setLoginip(rs.getString("LOGINIP"));
                admin.setLogintime(rs.getDate("LOGINTIME"));
                admin.setCreatetime(rs.getDate("CREATETIME"));
                return admin;
            }else{
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
